﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Collections.Specialized;

namespace EDMDesigner.Business.Util
{
    public class EventedObservableCollection<T> : ObservableCollection<T>
    {
        public EventedObservableCollection()
        {
        }

        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            base.OnCollectionChanged(e);
            if (e.NewItems != null)
                foreach (T newItem in e.NewItems)
                    OnItemAdded(newItem);
            if (e.OldItems != null)
                foreach (T oldItem in e.OldItems)
                    OnItemRemoved(oldItem);
        }

        protected virtual void OnItemAdded(T item)
        {
            if (ItemAdded != null)
                ItemAdded(item);
        }

        protected virtual void OnItemRemoved(T item)
        {
            if (ItemRemoved != null)
                ItemRemoved(item);
        }

        public event Action<T> ItemAdded;
        public event Action<T> ItemRemoved;
    }
}
